package shop.controller.front.home;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.services.front.account.AccountService;
import shop.services.front.account.bean.Account;
import shop.services.front.indexImg.IndexImgService;
import shop.services.front.indexImg.bean.IndexImg;
import shop.services.front.news.NewsService;
import shop.services.front.news.bean.News;
import shop.services.front.offline.OfflineService;
import shop.services.front.offline.bean.Offline;
import shop.services.front.offlineAccount.OfflineAccountService;
import shop.services.front.offlineAccount.bean.OfflineAccount;
import shop.services.front.organize.OrganizeService;
import shop.services.front.organize.bean.Organize;
import shop.services.front.systemSetting.SystemSettingService;
import shop.services.front.systemSetting.bean.SystemSetting;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/18.
 */
@Controller
@RequestMapping("/")
public class HomeController extends FrontBaseController<News> {
    @Resource
    private NewsService newsService;
    @Resource
    private IndexImgService indexImgService;
    @Resource
    private AccountService accountService;
    @Resource
    private OfflineAccountService offlineAccountService;
    @Resource
    private OrganizeService organizeService;
    @Resource
    private OfflineService offlineService;
    @Resource
    private SystemSettingService systemSettingService;
    @Override
    public Services<News> getService() {
        return newsService;
    }


    /**
     * 页面跳转方法
     *
     * @return
     */
    @RequestMapping({"/", "/index"})
    public String toIndex() {
        return "/front/login";
    }
    @RequestMapping("toHome")
    public String toHome(){return "/front/home/home";}
    @RequestMapping("/toNewsDetail")
    public String toNewsList() {
        return "/front/home/newsDetail";
    }
    @RequestMapping("/toAreaList")
    public String toAreaList() {
        return "/front/home/areaList";
    }

    /**
     * 跳转到公益时数明细页面
     * @return
     */
    @RequestMapping("/toServiceTimeDetail")
    public String toServiceTimeDetail() {
        return "/front/home/timeDetail";
    }

    /**
     * 跳转到爱优享介绍页面
     * @return
     */
    @RequestMapping("/toHomeDetail")
    public String toHomeDetail() {
        return "/front/home/homeDetail";
    }

    /**
     * 跳转到更多资讯列表页面
     * @return
     */
    @RequestMapping("/toMoreNews")
    public String toMoreNews() {
        return "/front/home/moreNews";
    }



    /**
     * 查询全部首页轮播图
     *
     * @param indexImg
     * @return
     */
    @RequestMapping(value = "/selectIndexImg", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectIndexImg(IndexImg indexImg) {
        JSONResult jsonResult = new JSONResult();
        List<IndexImg> list = indexImgService.selectList(indexImg);
        jsonResult.setData(list);
        jsonResult.setSuccess(true);
        return jsonResult;
    }


    /**
     * 获取当前登陆用户信息
     * @param session
     * @return
     */
    @RequestMapping(value = "/getSession",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult getSession(HttpSession session){
        JSONResult rs=new JSONResult();
        Map result=new HashMap<>();
        if (session.getAttribute("accountInfo")!=null){
        result.put("account",session.getAttribute("accountInfo"));
        }else {
            result.put("account",null);
        }
        if(session.getAttribute("organizeInfo")!=null){
        result.put("organize",session.getAttribute("organizeInfo"));
        }else {
            result.put("organize",null);
        }
        rs.setData(result);
        rs.setSuccess(true);
        return  rs;
    }


    @RequestMapping(value = "/updateSession",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult updateSession(HttpSession session){
        JSONResult rs=new JSONResult();
        Map result=new HashMap<>();
        if (session.getAttribute("accountInfo")!=null){
            Account account=(Account)session.getAttribute("accountInfo");
            account=accountService.selectOne(account);
            session.setAttribute("accountInfo",account);
            result.put("account",session.getAttribute("accountInfo"));
        }else {
            result.put("account",null);
        }
        if(session.getAttribute("organizeInfo")!=null){
            Organize organize=(Organize)session.getAttribute("organizeInfo");
            organize=organizeService.selectOne(organize);
            session.setAttribute("organizeInfo",organize);
            result.put("organize",session.getAttribute("organizeInfo"));
        }else {
            result.put("organize",null);
        }
        rs.setData(result);
        rs.setSuccess(true);
        return  rs;
    }

    /**
     * 查询公益时数记录
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectAstList")
    @ResponseBody
    public JSONResult selectAstList(HttpServletRequest request) throws Exception {
        Account account = (Account) request.getSession().getAttribute("accountInfo");
        Organize organize = (Organize) request.getSession().getAttribute("organizeInfo");
        if(account!=null){//判断登陆用户类型
            OfflineAccount offlineAccount=new OfflineAccount();
            offlineAccount.setAccountID(account.getId());
            List<OfflineAccount> rsList = offlineAccountService.selectAstList(offlineAccount);//查询活动参与记录
            for(OfflineAccount rs : rsList){
                rs.setSumServiceTime(rs.getServiceTime());
                offlineAccountService.update(rs);
            }
            jsonResult = new JSONResult();
            jsonResult.setData(rsList);
            return jsonResult;
        }else if(organize!=null){//判断登陆用户类型
           Offline offline =new Offline();
           offline.setOrganizeID(organize.getId());
           List<Offline> rsList=offlineService.selectList(offline);//查询发布的活动
           for(Offline rs : rsList){//循环遍历活动
               OfflineAccount offlineAccount = new OfflineAccount();
               offlineAccount.setOfflineID(rs.getId());
               List<OfflineAccount> offlineAccountList=offlineAccountService.selectAstList(offlineAccount);//查询参与记录
               if(!StringUtils.isBlank(rs.getServiceTime())){
                   rs.setSumServiceTime(String.valueOf(Integer.parseInt(rs.getServiceTime()) * offlineAccountList.size()));//计算活动总公益时数
                   offlineService.update(rs);
               }
           }
            jsonResult = new JSONResult();
            jsonResult.setData(rsList);
            return jsonResult;
        }else{
            jsonResult.setSuccess(false);
            jsonResult.setMessage("用户未登录，无法查看公益时数！");
            return jsonResult;
        }
    }

    /**
     * 查询爱优享介绍页
     * @param
     * @return
     */
    @RequestMapping(value = "/selectAbout", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectAbout() {
        SystemSetting setting = systemSettingService.selectOne(new SystemSetting());
        jsonResult = new JSONResult();
        jsonResult.setData(setting);
        return jsonResult;
    }

    /**
     * 查询通知内容
     * @param
     * @return
     */
    @RequestMapping(value = "/selectNotice", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectNotice() {
        SystemSetting setting = systemSettingService.selectOne(new SystemSetting());
        jsonResult = new JSONResult();
        jsonResult.setData(setting);
        return jsonResult;
    }

    /**
     * 查询帮助中心内容
     * @param
     * @return
     */
    @RequestMapping(value = "/selectHelp", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectHelp() {
        SystemSetting setting = systemSettingService.selectOne(new SystemSetting());
        jsonResult = new JSONResult();
        jsonResult.setData(setting);
        return jsonResult;
    }

    /**
     * 查询新闻列表
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectMoreNews")
    @ResponseBody
    public JSONResult selectMoreNews(HttpServletRequest request, @ModelAttribute("news") News news) throws Exception {
        List<News> rsList = newsService.selectList(news);
        jsonResult = new JSONResult();
        jsonResult.setData(rsList);
        return jsonResult;
    }

}